package dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.R;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import GsonBean.JcxxChakanBean;
import GsonBean.SuccessBean;
import GsonBean.post.ShPost;
import GsonBean.post.TjjdjcdPost;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import Utils.MyToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zx on 2017/4/14.
 */
//发出人【审核】--【确认】接口方法
public class ShDialog extends BaseDialog {

    JcxxChakanBean mJcxxChakanBean;

    RadioButton tongyi;
    RadioButton butongyi;
    EditText edit;
    TextView queding;
    TextView quxiao;

    ShPost mShPost;



    public ShDialog(Context context, JcxxChakanBean mJcxxChakanBean) {
        super(context);
        this.mJcxxChakanBean = mJcxxChakanBean;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_sh);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        tongyi = (RadioButton) findViewById(R.id.tongyi);
        butongyi = (RadioButton) findViewById(R.id.butongyi);
        edit = (EditText) findViewById(R.id.edit);
        queding = (TextView) findViewById(R.id.queding);
        quxiao = (TextView) findViewById(R.id.quxiao);
    }

    @Override
    protected void InitData() {
        mShPost = new ShPost();
    }

    @Override
    protected void InitListen() {

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShDialog.this.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (IsAllow()) {
                    MyApplication.ShowWaitDialog(getContext());
                    mShPost.setJdjhjcbId(mJcxxChakanBean.getRjdjhjcbInfo().getJdjhjcbId());
                    if (tongyi.isChecked()) {
                        mShPost.setWhetherAuditAgree("Y");
                    } else {
                        mShPost.setWhetherAuditAgree("N");
                    }
                    mShPost.setTgshyj(edit.getText().toString());
                    mShPost.setZrDwMc(mJcxxChakanBean.getRjdjhjcbInfo().getZrDwMc());
                    mShPost.setSfjytg(mJcxxChakanBean.getRjdjhjcbInfo().getSfjytg());
                    mShPost.setFldw(mJcxxChakanBean.getRjdjhjcbInfo().getFldw());
                    mShPost.setFlrName(mJcxxChakanBean.getRjdjhjcbInfo().getFlrName());
                    mShPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());

                    //审核单位流转信息list
                    List<TjjdjcdPost.LjyyxRjdjhjcbShdw> jcbShdwList=new ArrayList<>();
                    TjjdjcdPost.LjyyxRjdjhjcbShdw shdw=new TjjdjcdPost.LjyyxRjdjhjcbShdw();
                    shdw.setJdjhbBh(mJcxxChakanBean.getRjdjhjcbInfo().getRjdjhbBh());
                    shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                    shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    jcbShdwList.add(shdw);
                    mShPost.setJcbShdwList(jcbShdwList);

                    NetWorkApi.Sh(mShPost, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    try {
                                        MyApplication.HideWaitDialog();
                                        SuccessBean mBean = response.body();
                                        if (mBean.getMsg().equals("success") && mBean.getCount() != 0) {
                                            MyToast.showToast("审核成功");
                                            RefreshManager.Jdjc.RefreshJdjclrxx();
                                            RefreshManager.Jdjc.RefreshJdjcdcl();
                                            RefreshManager.Jdjc.RefreshJdjcycl();
                                            ShDialog.this.dismiss();
                                            getContext().startActivity(new Intent(getContext(), JclrActivity.class));
                                        } else {
                                            MyToast.showToast("审核失败，请检查网络信息");
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );

                }
            }
        });
    }


    public boolean IsAllow() {
        if (tongyi.isChecked()){
            return true;
        }

        if (butongyi.isChecked() && edit.getText().toString().equals(""))
        {
            MyToast.showToast("不同意必须填写理由");
            return false;
        }
        if (butongyi.isChecked() && !edit.getText().toString().equals(""))
        {
            return true;
        }

        MyToast.showToast("请将表格填写完整");
        return false;
    }


}
